package com.winton.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.winton.Z.Page;
import com.winton.dao.UserDao;
import com.winton.model.S_USER;
import com.winton.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	UserDao userDao;


	@Override
	public String insertUser(S_USER user) {
				
		return userDao.insertUser(user) ? "新增用户成功":"新增用户失败";
	}

	@Override
	public String updateUser(S_USER user) {
		
		return userDao.updateUser(user) ? "修改用户成功":"修改用户失败";
	}

	@Override
	public Map<String,Object> queryUserList(S_USER user,int pageon) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		Page page = new Page(pageon);
		StringBuffer strWhere = new StringBuffer();
		String str = "";			
		if(user.getUsername()!=null && !"".equals(user.getUsername())) {			
			strWhere.append(" and username like '%'||?||'%' ");				
			str += user.getUsername();		
		}					
		if(user.getStarttime()!=null && !"".equals(user.getStarttime()) ) {					
			strWhere.append(" and systime>= to_date( ? ,'yyyy-mm-dd hh24:mi:ss') ");			
			str = str.length()>0 ? str+","+user.getStarttime() : str+user.getStarttime();			
		}
		if(user.getEndtime()!=null && !"".equals(user.getEndtime()) ) {									
			strWhere.append(" and systime<= to_date( ? ,'yyyy-mm-dd hh24:mi:ss')+1 ");							
			str = str.length()>0 ? str+","+user.getEndtime() : str+user.getEndtime();
		}			
		String[] s = str.split(",");
		
		if(s.length==1) {
			if(str.length()>0) {
				Object[] objArr = {s[0]};				
			    page.setRowcountAndCompute(userDao.selectUserListCount(strWhere.toString(),objArr));
			    map.put("page", page);
			    map.put("list", userDao.queryUserList(page,strWhere.toString(),objArr)); 
			}else {
				Object[] objArr = {};
			    page.setRowcountAndCompute(userDao.selectUserListCount(strWhere.toString(),objArr));
			    map.put("page", page);
			    map.put("list", userDao.queryUserList(page,strWhere.toString(),objArr)); 
				
			}			 
		}else if(s.length==2) {
			Object[] objArr = {s[0],s[1]};
		    page.setRowcountAndCompute(userDao.selectUserListCount(strWhere.toString(),objArr));
		    map.put("page", page);
		    map.put("list", userDao.queryUserList(page,strWhere.toString(),objArr)); 
		}else if(s.length==3) {
			Object[] objArr = {s[0],s[1],s[2]};
		    page.setRowcountAndCompute(userDao.selectUserListCount(strWhere.toString(),objArr));
		    map.put("page", page);
		    map.put("list", userDao.queryUserList(page,strWhere.toString(),objArr)); 
		}else {
			
		}										 		
		return map;
	}

	@Override
	public String updateUserDeleted(S_USER user) {
		
		return userDao.updateUserDeleted(user) ? "启用成功":"启用失败";
	}

	@Override
	public int deleteUserByUserId(int id) {
		
		int num = userDao.deleteUserItem(id);		
		return num>0 ? userDao.deleteUserByUserId(id):0 ;
	}

	@Override
	public S_USER findUserByUserId(int id) {
		
		return userDao.findUserByUserId(id);
	}

	@Override
	public int loginidVerify(String loginid,int id) {
		List<S_USER> list = userDao.loginidVerify(loginid);
		if(id==0) {
			return ( list!=null && list.size() > 0 ) ? 0:1;
		}else {
			if( list!=null && list.size() > 0 ) {
				
				return (list.get(0).getId()==id) ? 1 : 0;
			}else {
				return 1;
			}
			
		}
						
	}

	@Override
	public S_USER userLogin(S_USER user) {
				
		return userDao.userLogin(user) ;
	}

	@Override
	public int updateUserItem(int userid, String strid) {
																							
		 userDao.deleteUserItem(userid);													
		return  userDao.addUserItem(userid,strid);
	}

	@Override
	public int deleteUserItem(int userid) {
		
		return userDao.deleteUserItem(userid);
	}

	@Override
	public int updatePassword(int id, String newpassword) {
		
		return userDao.updatePassword(id,newpassword);
	}

}
